package www.dashu.com;

import www.dashu.com.config.FreeMarkerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootApplication
public class GenerateDbaApplication
{

    @Autowired
    FreeMarkerUtil freeMarkerUtil;

    public static void main(String[] args)
    {
        SpringApplication.run(GenerateDbaApplication.class, args);
        System.exit(-1);
    }

    @PostConstruct
    public void init() throws Exception
    {
        //生成路径，根据实际情况修改即可
        //生成文件包名，根据实际情况修改即可
        String basePackageUrl = "www.dashu.com";
        String tableSchema = "smart_mall";

//        List<String> tables = Arrays.asList("sys_role_perms");
        List<Map<String, Object>> tables = freeMarkerUtil.getTables("oms_");

        String _path = System.getProperty("user.dir");
        freeMarkerUtil.clearFile(_path + "/mall-freemaker/src/main/java/www/dashu/com/domain/");
        freeMarkerUtil.clearFile(_path + "/mall-freemaker/src/main/java/www/dashu/com/dto/");
        freeMarkerUtil.clearFile(_path + "/mall-freemaker/src/main/java/www/dashu/com/dao/");
        freeMarkerUtil.clearFile(_path + "/mall-freemaker/src/main/java/www/dashu/com/service/");
        freeMarkerUtil.clearFile(_path + "/mall-freemaker/src/main/java/www/dashu/com/controller/");
        freeMarkerUtil.clearFile(_path + "/mall-freemaker/src/main/java/www/dashu/com/web/views/");
        freeMarkerUtil.clearFile(_path + "/mall-freemaker/src/main/java/www/dashu/com/web/js/api/");
        freeMarkerUtil.clearFile(_path + "/mall-freemaker/src/main/java/www/dashu/com/web/js/router/");

        for(Map<String, Object> mapItem : tables){
            //bean类名
            String tableName = String.valueOf(mapItem.get("name"));
            String entityName = freeMarkerUtil.getEntityName(tableName);

            //封装参数
            Map<String, Object> root = new HashMap<>();
            root.put("basePackageUrl", basePackageUrl);
            //表参数
            root.put("tableName", tableName);
            root.put("tableComment", String.valueOf(mapItem.get("comment")));
            root.put("entityName", entityName);

            String _t = tableName.substring(tableName.indexOf("_") + 1);
            String pa = freeMarkerUtil.underlineToHump(_t);

            root.put("contextUri", pa);

            root.put("entityNameLower", freeMarkerUtil.getEntityNameLower(tableName));
            root.put("columns", freeMarkerUtil.getDataInfo(tableName, tableSchema));

            // 生成bean
            freeMarkerUtil.generate(root, "entity.ftl", "domain", entityName + ".java");
            // 生成query dto
            freeMarkerUtil.generate(root, "dto.ftl", "dto", entityName + "Query.java");
            // 生成dao
            freeMarkerUtil.generate(root,"dao.ftl","dao",entityName+"Dao.java");
            // 生成service
            freeMarkerUtil.generate(root,"service.ftl","service",entityName+"Service.java");
            //生成controller
            freeMarkerUtil.generate(root,"controller.ftl","controller",entityName+"Controller.java");
            //生成页面
            freeMarkerUtil.generate(root,"vue.ftl","web/views",pa+".vue");
            //js
            freeMarkerUtil.generate(root,"api-js.ftl","web/js/api",pa+".js");
//            freeMarkerUtil.generate(root,"router-js.ftl","web/js/router","router.js");


        }

    }

}